package com.cys.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cys.bean.WeatherStations;
import com.cys.db.DBHelper;

/**
 * Servlet implementation class HWeatherStationsServlet
 */
@WebServlet("/HWeatherStationsServlet")
public class HWeatherStationsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HWeatherStationsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			//Connection conn = DriverManager.getConnection(url, username, password);
			Connection conn=DBHelper.getConn();
			//System.out.println(conn);
			// 获取Statement
			Statement stmt = conn.createStatement();
			// 添加图书信息的SQL语句
			String sql = "select * from t_log_weather_station order by data_time limit 10";
			// 执行查询
			ResultSet rs = stmt.executeQuery(sql);
			//System.out.println(rs);
			// 实例化List对象
			List<WeatherStations> list = new ArrayList<WeatherStations>();
			// 判断光标向后移动，并判断是否有效
			while (rs.next()) {
				// 实例化Book对象
				WeatherStations wSdata = new WeatherStations();
				wSdata.setDev_weather_station(rs.getInt("dev_weather_station"));
				wSdata.setData_time(rs.getString("data_time"));
			wSdata.setWind_speed(rs.getFloat("Wind_speed"));
			wSdata.setWind_origent(rs.getInt("wind_origent"));
				wSdata.setRightess(rs.getFloat("rightess"));
				wSdata.setTemperlature(rs.getFloat("temperlature"));
				wSdata.setWetness(rs.getFloat("wetness"));
				list.add(wSdata);
				System.out.println(list.size());
			}
			// 将图书集合放置到request之中
			request.setAttribute("list", list);
			rs.close(); // 关闭ResultSet
			stmt.close(); // 关闭Statement
			conn.close(); // 关闭Connection
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 请求转发到book_list.jsp
		request.getRequestDispatcher("HistoricalData.jsp").forward(request, response);
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
